<template>
  <div class="app-container">

    <!-- 查询和其他操作 -->
    <div class="filter-container">
      <el-form :inline="true">

        <el-form-item label="机构">
          <el-select v-model="listQuery.departmentId" placeholder="请选择">
            <el-option
              v-for="item in departmentList"
              :key="item.id"
              :label="item.name"
              :value="item.id"/>
          </el-select>
        </el-form-item>

        <el-form-item label="帐号名称">
          <el-input v-model="listQuery.username" clearable class="filter-item" placeholder="帐号名称"/>
        </el-form-item>

        <!-- <el-form-item label="状态">
          <el-select v-model="listQuery.status" placeholder="请选择" style="width: 90px;">
            <el-option
              v-for="item in status_options"
              :key="item.value"
              :label="item.label"
              :value="item.value"/>
          </el-select>
        </el-form-item> -->

        <el-button class="filter-item" type="primary" icon="el-icon-search" @click="handleFilter">查找</el-button>

        <el-button class="filter-item" style="float:right" type="primary" icon="el-icon-plus" @click="startAdd">添加</el-button>
      </el-form>
    </div>

    <!-- 查询结果 -->
    <el-table v-loading="listLoading" :data="list" element-loading-text="正在查询中。。。" border fit highlight-current-row >

      <el-table-column align="center" label="帐号名称" prop="username" />

      <el-table-column align="center" label="所属机构" prop="departmentName"/>

      <!-- <el-table-column align="center" label="状态" prop="status" >
        <template slot-scope="scope">
          <el-tag >{{ getStatus(scope.row.status) }}</el-tag>
        </template>
      </el-table-column> -->

      <el-table-column align="center" label="操作" width="180" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button type="primary" size="small" @click="startEditAdmin(scope.row)">编辑</el-button>
          <el-button type="primary" size="small" @click="deleteAdminFunc(scope.row.username, scope.row.id)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total>0" :total="total" :page.sync="listQuery.page" :limit.sync="listQuery.pageSize" @pagination="getList" />

    <el-dialog :close-on-click-modal="false" :title="dialogType == 1?'添加管理员':'修改管理员'" :visible.sync="dialogFormVisible" width="28%">
      <el-form ref="dialogForm" :inline="true" :rules="formRules" :model="dialogForm" align="center" label-width="120px">
        <el-form-item label=" 机构名称  " prop="departmentId">
          <el-select v-model="dialogForm.departmentId" :disabled="dialogType == 2" placeholder="请选择">
            <el-option
              v-for="item in departmentList"
              :key="item.id"
              :label="item.name"
              :value="item.id"/>
          </el-select>
        </el-form-item>
        <el-form-item label="帐号名称  " prop="username">
          <el-input v-model="dialogForm.username" clearable class="filter-item" style="max-width: 260px;" placeholder=""/>
        </el-form-item>
        <el-form-item label="帐号密码  " prop="password">
          <el-input v-model="dialogForm.password" clearable class="filter-item" style="max-width: 260px;" placeholder=""/>
        </el-form-item>
      </el-form>

      <div slot="footer" class="dialog-footer" align="center">
        <el-button @click="confirmEdit('dialogForm')">确定</el-button>
        <el-button @click="dialogFormVisible = false">取消</el-button>
      </div>
    </el-dialog>

  </div>
</template>

<script>
/* eslint-disable eqeqeq */

import { listAdmin, deleteAdmin, createAdmin, updateAdmin } from '@/api/admin'
import { getDepartmentList } from '@/api/base'
import Pagination from '@/components/Pagination' // Secondary package based on el-pagination

export default {
  name: 'Admin',
  components: { Pagination },
  data() {
    return {
      list: null,
      total: 0,
      listLoading: true,
      status_options: [{
        value: '',
        label: '全部'
      }, {
        value: '1',
        label: '启用'
      }, {
        value: '2',
        label: '停用'
      }],
      listQuery: {
        page: 1,
        pageSize: 20
      },
      downloadLoading: false,
      dialogFormVisible: false,
      dialogForm: {},
      dialogType: 1,
      formRules: {
        username: [{ required: true, message: '请输入帐号名称', trigger: 'blur' }],
        password: [{ required: true, message: '请输入帐号密码', trigger: 'blur' }, { min: 6, message: '密码长度不能小于6个字符', trigger: 'blur' }],
        departmentId: [{ required: true, message: '请选择机构名称', validator: this.checkSelected, trigger: 'change' }]
      },
      departmentList: []
    }
  },
  created() {
    getDepartmentList().then(res => {
      this.departmentList = res.data.data
      this.getList()
    })
  },
  methods: {
    // 校验下拉框
    checkSelected(rule, value, callback) {
      if (!value) {
        callback(new Error(rule.message))
      } else {
        callback()
      }
    },
    getList() {
      this.listLoading = true
      listAdmin(this.listQuery).then(response => {
        this.list = response.data.data.list
        this.total = response.data.data.total
        this.listLoading = false
      }).catch(() => {
        this.list = []
        this.total = 0
        this.listLoading = false
      })
    },
    handleFilter() {
      this.listQuery.page = 1
      this.getList()
    },
    // getStatus(status) {
    //   if (status == 1) {
    //     return '启用'
    //   }
    //   return '停用'
    // },
    deleteAdminFunc(username, adminId) {
      this.$confirm(`您确定删除账号：${username}`, '温馨提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        deleteAdmin({ id: adminId }).then(response => {
          if (response.data) {
            this.$message({
              message: '删除成功!',
              type: 'success'
            })
            this.getList()
          }
        })
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        })
      })
    },
    startAdd() {
      this.dialogForm = {}
      this.dialogFormVisible = true
      this.dialogType = 1
      this.$nextTick(() => {
        this.$refs.dialogForm.clearValidate()
      })
    },

    confirmEdit(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          if (this.dialogType == 1) {
            createAdmin(this.dialogForm).then(response => {
              if (response.data) {
                this.$message({
                  message: '添加成功!',
                  type: 'success'
                })
                this.getList()
                this.dialogFormVisible = false
                this.dialogForm = {}
              }
            })
          } else {
            updateAdmin(this.dialogForm).then(response => {
              if (response.data) {
                this.$message({
                  message: '修改成功!',
                  type: 'success'
                })
                this.getList()
                this.dialogFormVisible = false
                this.dialogForm = {}
              }
            })
          }
        } else {
          console.log('error submit!!')
          return false
        }
      })
    },
    startEditAdmin(row) {
      this.dialogFormVisible = true
      this.dialogType = 2
      this.dialogForm = {...row}
      this.$nextTick(() => {
        this.$refs.dialogForm.clearValidate()
      })
    }

  }

}
</script>
